
package com.acorn.community.common.api;

import com.acorn.community.common.exception.CodeMsg;
import com.acorn.community.common.exception.ICodeMsg;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel(
    value = "Result",
    description = "标准返回体"
)
public class Resp<T> implements Serializable {
    private static final long serialVersionUID = 5858245386747030579L;
    @ApiModelProperty("返回码")
    private String requestId;
    @ApiModelProperty("返回码")
    private int code;
    @ApiModelProperty("返回信息")
    private String msg;
    @ApiModelProperty("返回数据")
    private T data;

    public Resp() {
        this.code = CodeMsg.SUCCESS.getCode();
        this.msg = CodeMsg.SUCCESS.getMessage();
    }

    public Resp(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Resp(ICodeMsg<Integer> codeMsg) {
        if (codeMsg != null) {
            this.code = (Integer)codeMsg.getCode();
            this.msg = codeMsg.getMessage();
        }

    }

    public Resp(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Resp(T data) {
        this.code = CodeMsg.SUCCESS.getCode();
        this.msg = CodeMsg.SUCCESS.getMessage();
        this.data = data;
    }

    public static <T> Resp<T> success(T data) {
        return new Resp(data);
    }

    public static Resp success() {
        return new Resp(CodeMsg.SUCCESS);
    }

    public static Resp error() {
        return new Resp(CodeMsg.ERROR);
    }

    public static Resp error(ICodeMsg<Integer> codeMsg) {
        return new Resp(codeMsg);
    }

    public static <T> Resp<T> error(int code, String msg) {
        return new Resp(code, msg);
    }

    public static <T> Resp<T> error(int code, String msg, T data) {
        return new Resp(code, msg, data);
    }

    public String getRequestId() {
        return this.requestId;
    }

    public int getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public void setRequestId(final String requestId) {
        this.requestId = requestId;
    }

    public void setCode(final int code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setData(final T data) {
        this.data = data;
    }

}
